/**
 *添加区域范围
 */
'use strict';

const conn = require('../../libs/DB/Conn');
const operationRecord = require("../sys/operationRecordAction");

module.exports = async function (params) {
    let insertObj ={
        parent_id: params.parentId ? params.parentId : 0,
        range_name:params.rangeName,
        user_id:params.leaderId,
        domain_id:params.userDomainId,
        height:params.height ? params.height : 0,
        top_level_id:params.topLevelId ? params.topLevelId : 0
    }
    if(params.lng && params.lat){
        insertObj.lng = params.lng;
        insertObj.lat = params.lat;
    }
    let insertSql = `INSERT INTO range_file SET ?`;
    let insertResult = await conn(insertSql, [insertObj]);
    if(!params.parentId){
        await conn(`UPDATE range_file SET top_level_id = ${insertResult.insertId} WHERE range_id = ${insertResult.insertId}`)
    }
    await operationRecord(params);
    return { success: true };
};
